<?php

namespace app\admin\validate\factor;

use app\admin\model\factor\FactorReview;
use think\Validate;

class AccountAuthentication extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
        'factor_id' => 'require|checkFactorId',
        'category_id' => 'require|checkCategoryId',
        'description' => 'require',
        'producer_name' => 'require',
        'province' => 'require',
        'city' => 'require',
        'area' => 'require',
        'address' => 'require',
        'legal_person' => 'require',
        'auth_person' => 'require',
        'contact_phone' => 'require',
        'enterprise_profile' => 'require',
        'product_name' => 'require',
        'product_specification' => 'require',
        'product_function' => 'require',
        'product_introduction' => 'require',
        'file_base64' => 'require',
        'quantification_method' => 'require|in:1,2',
    ];

    /**
     * 提示消息
     */
    protected $message = [
        'factor_id.require' => '请选择碳排放数据库',
        'category_id.require' => '请选择因子分类',
        'description.require' => '请填写产品描述',
        'producer_name.require' => '请填写生产者名称',
        'province.require' => '请填写省份',
        'city.require' => '请填写城市',
        'area.require' => '请填写区县',
        'address.require' => '请填写详细地址',
        'legal_person.require' => '请填写法定代表人',
        'auth_person.require' => '请填写授权人',
        'contact_phone.require' => '请填写联系电话',
        'enterprise_profile.require' => '请填写企业概况',
        'product_name.require' => '请填写产品名称',
        'product_specification.require' => '请填写产品规格型号',
        'product_function.require' => '请填写产品功能',
        'product_introduction.require' => '请填写产品介绍',
        'quantification_method.require' => '请选择量化方法',
        'quantification_method.in' => '请选择量化方法',
        'file_base64.require' => '请上传文件',
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => [],
        'edit' => [],
        'cancelAccountAuth' => ['factor_id'],
        'setFactorUnit' => ['factor_id'],
        'reviewFactor' => ['factor_id','category_id','description'],
        'getFactorReview' => ['factor_id'],
        'factorAccountAuthDetail' => ['factor_id'],
        'getAccountPdf' => ['file_base64'],
        'factorAccountAuth' => ['factor_id'],

    ];

    public function checkFactorId($value, $rule, $data)
    {
        $factor = \app\admin\model\factor\CarbonFactor::where('id', $value)->find();
        if (!$factor) {
            return '数据不存在';
        }
        if ($this->currentScene == 'factorAccountAuth' ) {
            if ($factor->is_account == 1) {
                return '该数据已核算认证';
            }
            $lastAuth = $factor->getLastAccountAuthAttr();
            if ($lastAuth) {
                if ($lastAuth->status == 0) {
                    return '数据核算认证审核中，请勿重复提交';
                } else if ($lastAuth->status == 1) {
                    return '该数据已核算认证';
                }
            }
        }
        if ($this->currentScene == 'reviewFactor') {
            if ($factor->is_account != 1) {
                return '该数据未核算认证不能上架';
            }
            $last = FactorReview::where('factor_id', $value)->order('createtime', 'desc')->find();
            if (!$last) {
                return true;
            }
            if ($last->status == 0) {
                return '审核中，请勿重复提交';
            }
//            if ($last->status == 1 || $factor->product_review == 1) {
//                return '该数据已上架';
//            }
        }
        return true;
    }

    public function checkCategoryId($value, $rule, $data)
    {
        $category = \app\admin\model\factor\Category::where('id', $value)->find();
        if (!$category) {
            return '数据不存在';
        }
        return true;
    }
    
}
